package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.admin.AdminConstans;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mappers.ApUserMapper;
import com.usian.user.mappers.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends BaseServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {
    @Autowired
   private ArticleFeign articleFeign;
    @Autowired
    private WemediaFeign wemediaFeign;

    @Autowired
    private ApUserMapper userMapper;


    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {
        //参数为空
        if (dto == null) return errorParam;

        //检查参数
       return gePageDate(dto,Wrappers.<ApUserRealname>lambdaQuery().eq(dto.getStatus()!=null,ApUserRealname::getStatus,dto.getStatus()));
    }

    @Override
    @GlobalTransactional
    public ResponseResult updateStatusById(AuthDto dto, Short status) {
        //判断dto对象是否为空
        if (dto==null||dto.getId()==null)return errorParam;
        //查出认证信息
        ApUserRealname  authentica = getById(dto.getId());
        //根据认证表的ap端的id查出ap端的用户信息
        ApUser apUser = userMapper.selectById(authentica.getUserId());
        //创建认证对象
        ApUserRealname realname = new ApUserRealname();
        //写入dto传入的信息
        realname.setStatus(status);
        realname.setId(dto.getId());
        //判断msg是否有不通过原因语如果有赋值给认证对象(realname)
       if (StringUtils.isNotEmpty(dto.getMsg()))realname.setReason(dto.getMsg());
        //修改认证对象状态还有不通过的原因
        updateById(realname);
        //如果status等于9通过就会执行if判断里面的代码
        if (status== AdminConstans.PASS_AUTH){
            //根据apUser对象的name进行查询,看看是否已经有自媒体账户进行注册防止账户重复
            WmUser wmUsera = wemediaFeign.findByName(apUser.getName());
            if (wmUsera==null){
                WmUser   wmUser = new WmUser();
                wmUser.setApUserId(authentica.getUserId());
                wmUser.setCreatedTime(new Date());
                wmUser.setStatus(9);
                BeanUtils.copyProperties(apUser,wmUser);
                //进行添加,方法执行完之后,mp会自动给对象进行反向赋值
                wemediaFeign.save(wmUser);
                wmUsera=wmUser;
            }
            //使用ap端用户id进行查询
            ApAuthor byUserId = articleFeign.findByUserId(apUser.getId());
            //判断是否有已经有自媒体账户
            if (byUserId==null){
                //没有的话添加账户
                ApAuthor apAuthor = new ApAuthor();
                apAuthor.setName(apUser.getName());
                apAuthor.setUserId(apUser.getId());
                apAuthor.setWmUserId(wmUsera.getId());
                apAuthor.setType(2);
                articleFeign.save(apAuthor);
            }
            //修改apUser的用户状态,给他设置成自媒体用户
            ApUser tempUser = new ApUser();
            tempUser.setId(apUser.getId());
            tempUser.setFlag((short) 1);
            userMapper.updateById(tempUser);
        }
      //  int a=1/0;
        return success;
    }
}